<!--
 * Copyright © 2023
 * @Author: zhangxiang
 * @Date: 2023-07-18 11:23:51
 * @LastEditors: zhangxiang
 * @LastEditTime: 2023-07-18 11:36:52
 * @Description:
 *
-->

<template>
  <el-form
    ref="form"
    class="create-huawei"
    :rules="rules"
    :model="form"
    label-width="auto"
    size="medium"
  >
    <el-form-item class="item" label="云服务商" prop="cloudProvider">
      <el-select
        v-model="form.cloudProvider"
        placeholder="请选择云服务商"
        style="width: 100%"
        :disabled="true"
      >
        <el-option
          v-for="item in cloudProviderOptions"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        />
      </el-select>
    </el-form-item>
    <el-form-item class="item right" label="云账户" prop="cloudAccountName">
      <el-select
        v-model="form.cloudAccountName"
        placeholder="请选择云账户"
        style="width: 100%"
        no-data-text="请先选择合适的云服务商"
        :loading="loadingCloudAccountName"
      >
        <el-option
          v-for="item in cloudAccountNameOptions"
          :key="item.cloudAccountName"
          :label="item.cloudAccountName"
          :value="item.cloudAccountName"
        />
      </el-select>
    </el-form-item>
    <el-form-item label="安全组名称" prop="name">
      <el-row type="flex" align="middle">
        <el-input v-model="form.name" placeholder="请输入安全组名称" />
        <el-popover placement="right" width="250" trigger="hover">
          <p v-html="nameRule"></p>
          <i
            slot="reference"
            class="el-icon-info icon"
            style="font-size: 18px; margin-left: 10px"
          ></i>
        </el-popover>
      </el-row>
    </el-form-item>
    <el-form-item label="区域" prop="cpRegionId">
      <el-select
        v-model="form.cpRegionId"
        placeholder="请选择区域"
        no-data-text="请选择合适的云服务商"
        style="width: 100%"
      >
        <el-option
          v-for="item in regionOptions"
          :key="item.cpId"
          :label="item.desc"
          :value="item.cpId"
        />
      </el-select>
    </el-form-item>
    <el-form-item label="描述" prop="desc">
      <el-input
        v-model="form.desc"
        placeholder="请输入描述"
        type="textarea"
        :rows="3"
      />
    </el-form-item>
  </el-form>
</template>

<script>
import resSelectorMixin from '@/views/mixin/resSelector.js'
import { getRegionList } from '@/api/cn-resource'

const NAME_RULE =
  '长度范围为1-64位，只能由中文、英文字母、数字、下划线、中划线、点组成'

export default {
  name: 'CreateHuawei',
  components: {},
  mixins: [resSelectorMixin],
  props: {
    cloudSelect: {
      type: String,
      default: '华为云'
    }
  },
  data() {
    return {
      nameRule: NAME_RULE,
      cloudProviderType: 'public',
      regionOptions: [],
      form: {
        cloudProvider: '',
        cloudAccountName: '',
        name: '',
        cpRegionId: '', // 区域
        desc: ''
      },
      // 校验规则
      rules: {
        cloudProvider: [
          { required: true, message: '请选择云服务商', trigger: 'change' }
        ],
        cloudAccountName: [
          { required: true, message: '请选择云账户', trigger: 'change' }
        ],
        name: [
          { required: true, message: '请输入安全组名称', trigger: 'blur' },
          {
            pattern: /^[\u4e00-\u9fa5\-\._a-zA-Z0-9]{1,64}$/,
            message: NAME_RULE
          }
        ],
        cpRegionId: [
          { required: true, message: '请选择区域', trigger: 'change' }
        ]
      }
    }
  },
  computed: {},
  watch: {
    /**
     * @description: 云商监听
     * @return {*}
     */
    'form.cloudProvider': {
      handler(newVal) {
        if (newVal) {
          // 获取区域
          this.form.cpRegionId = ''
          this.getRegionList()
          this.form.cpVpcId = ''
        }
      },
      immediate: true
    },
    cloudSelect: {
      handler(newVal) {
        if (newVal) {
          this.form.cloudProvider = this.cloudSelect
        }
      },
      immediate: true
    }
  },
  mounted() {},
  methods: {
    /**
     * @description: 获取区域
     * @return {*}
     */
    getRegionList() {
      getRegionList({
        keyword: this.form.cloudProvider,
        pageNum: 1,
        pageSize: 100
      }).then((res) => {
        if (res.data) {
          this.regionOptions = Array.isArray(res.data.data)
            ? res.data.data
            : []
        } else {
          this.regionOptions = []
        }
      })
    },
    /**
     * @description: 校验
     * @return {*}
     */
    validateForm(callback) {
      this.$refs.form.validate((valid, obj) => {
        if (valid) {
          callback(this.form)
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
@import "@/assets/css/common.less";
.create-huawei {
  .item {
    display: inline-block;
    width: calc(50% - 10px);
    &.right {
      margin-left: 20px;
    }
  }
}
</style>
